<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
require_once ZEAI.'cache/config_crm.php';
require_once ZEAI.'sub/ZeaiUP.php';
if(!CRMQX('crm_talk_tj')){exit(noauth('暂无【外呼统计报表】权限'));}
function show_his($num){
	if($num>0){
		$tj_zd=intval($num);
		$whs_h=floor($num/3600);	
		$whs_i=floor(($num-$whs_h*3600)/60);	
		$whs_s=$num-$whs_h*3600-$whs_i*60;	
	}else{
		return '0秒';
	}
	$times_str='';
	if($whs_h>0){$times_str.=$whs_h.'时';}
	if($whs_i>0 || $whs_h>0){$times_str.=$whs_i.'分';}
	if($whs_s>0 || $whs_i>0 || $wh_h>0){$times_str.=$whs_s.'秒';}
	if(empty($times_str))$times_str='0秒';
	return $times_str;
}
$SQL="";
if(empty($sDATE1) && empty($sDATE2)){	
	$SQL .= " flag>0";
}else{
	if(!empty($sDATE1)){
		$sDATE1 = $sDATE1.' 00:00:00';
		if(!empty($SQL))$SQL.=" and ";
		$SQL .= " ( addtime >= '$sDATE1' )";
	}
	if(!empty($sDATE2)){
		$sDATE2 = $sDATE2.' 23:59:59';
		if(!empty($SQL))$SQL.=" and ";
		$SQL .= " ( addtime <= '$sDATE2' )";
	}
}

if (ifint($qxflag) && $qxflag>0){$SQL .= " AND flag = $qxflag ";}
$Skey  = trimhtml($Skey);
$Skey2 = trimhtml($Skey2);
if (ifint($Skey)){
	$SQL .= " AND admid=$Skey  ";// OR admname like '%".$Skey."%'
}elseif(!empty($Skey)){
	$SQL .= " AND (admname LIKE '%".$Skey."%')";
}
//超管搜索按门店
if (ifint($agentid) && CRM_ADM){
	$SQL .= " AND agentid=$agentid";
}

if(!empty($Skey2)){
	if(ifmob($Skey2)){
		$SQL .= " AND ( mob= '".$Skey2."')";
	}elseif(ifint($Skey2)){
		$SQL .= " AND (  uid = '".$Skey2."')";
	}else{
		$SQL .= " AND ( name LIKE '%".$Skey2."%' or mob like '%".$Skey2."%' )";
	}
}
switch ($sort) {
	default:$SORT = " ORDER BY id DESC ";break;
}
$rt = $db->query("SELECT admid,uid,max(admname) as admname,count(id) as znum,sum(talktime) as ztalktime FROM ".__TBL_TALK__." WHERE ".$SQL." group by admid ".$SORT." LIMIT ".$_ADM['admLimit']);
$total = $db->num_rows($rt);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/cache/udata.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="<?php echo HOST;?>/res/select4.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>qxflag_ARR=<?php echo $_CRM['qxflag'];?>;</script>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<link href="css/crm.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
</head>
<style>
td.border0{vertical-align:top;padding-top:10px;line-height:12px}
img.photo_s{width:60px;height:60px;display:block;margin:12px auto;border-radius:40px;object-fit:cover;-webkit-object-fit:cover;cursor:zoom-in}
.pathlist img{margin:8px 5px 5px 2px;width:30px;height:30px;object-fit:cover;-webkit-object-fit:cover;border-radius:2px}
.box{padding-top:10px;padding-bottom:20px;min-width:1200px}
.top4,.top4 li,.top4,.top4 li i{box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box}
.top4{width:100%;min-width:1200px;padding:0 15px;margin-top:20px}
.top4 li{width:25%;float:left;width:calc(25% - 20px);width:-webkit-calc(25% - 20px);margin-right:20px;min-width:250px;height:91px;padding:10px;background-color:#fff;position:relative;color:#999;border-radius:4px}
.top4 li:last-child{width:25%;margin-right:0}
.top4 li i{position:absolute;left:15px;top:15px;width:60px;border-radius:30px;height:60px;font-size:30px;line-height:60px;color:#fff;background-color:#009688/*border:1px solid #FF5722*/}
.top4 li em{font-size:13px;padding-left:80px;text-align:left;padding-top:8px}
.top4 li em b{display:inline-block;font-weight:bold;font-size:26px;color:#009688;font-family:Arial}
.top4 li em font.hot{font-size:13px;color:#EE5A4E}
.top4 li .liRnum{font-size:13px;color:#666;margin-left:10px;display:inline-block}
.top4 li .liRnum b{font-size:16px;color:#009688;margin:0 5px;}
.top4 li .liRnum b,.top4 li .liRnum font{vertical-align:middle}
</style>
<body>
<div class="top4">
	<li>
	<i class="ico">总</i>
	<em>
        <b><?php $tj_za=$db->COUNT(__TBL_TALK__,$SQL); echo $tj_za;?></b> 次</font>
        <span><br><font class="h6b">总呼出数</font>(次)
        </span>
	</em>
    </li>
	<li>
    	<i class="ico">接</i>
		<em>
			<b><?php $tj_zb=$db->COUNT(__TBL_TALK__,$SQL.' and flag=4');echo $tj_zb;?></b> 次
            <span><br><font class="h6b">接通数</font>（次）</span>
        </em>
    </li>
	
	<li>
    	<i class="ico">效</i>
		<em>
			<b><?php 
			$tj_zc = $db->COUNT(__TBL_TALK__,$SQL.' and flag=4 and talktime>30','name');			
			echo $tj_zc;?></b> 次
            <span><br><font class="h6b">有效数</font>（30秒）</span>
        </em>
    </li>    
    <li>
    	<i class="ico">时</i>
		<em>
			<b><?php 
			$tj_zd=0;
			$cxrow = $db->ROW(__TBL_TALK__,"sum(talktime) as ztalktime",$SQL." and flag=4",'name');			
			$tj_zd=intval($cxrow['ztalktime']);
			echo show_his($tj_zd);		 		
			?></b>
            <span><br><font class="h6b">总时长</font></span>(秒)
        </em>
    </li>
</div>
<div class="clear"></div>
<div class="top4" style="margin-top:15px">
    <li>
    	<i class="ico">通</i>
		<em>
			<b><?php echo ($tj_za>0)?sprintf("%.2f",($tj_zb*100/$tj_za)):'0.00';?></b> %
            <span><br><font class="h6b">接通率</font>（%）</span>
        </em>
    </li>
    <li>
    	<i class="ico">有</i>
		<em>
			<b><?php echo ($tj_zb>0)?sprintf("%.2f",($tj_zc*100/$tj_zb)):'0.00';?></b> %
            <span><br><font class="h6b">有效接通率</font>（30秒）</span>
        </em>
    </li>    
    <li>
    	<i class="ico">均</i>
		<em>
			<b><?php echo ($tj_za>0)?show_his(intval($tj_zd/$tj_za)):'0秒';?></b>
            <span><br><font class="h6b">平均时长</font>（秒）</span>
        </em>
    </li>
    <li>
    	<i class="ico">未</i>
		<em>
			<b><?php echo notel_num(0); ?></b> 人
            <span><br><font class="h6b">未拔打剩余客户数量</font>（人）</span>
        </em>
    </li>
</div>
<div class="clear"></div>

<div class="box">
    <div class="topsearch">
    <form name="ZEAI_CN__form1" method="get" action="<?php echo SELF; ?>" style="margin-right:20px">
          <!--超管按门店查询-->
          <?php if(CRM_ADM){?>
          <?php
            $rt2=$db->query("SELECT id,title FROM ".__TBL_CRM_AGENT__." WHERE flag=1 ORDER BY px DESC,id DESC");
            $total2 = $db->num_rows($rt2);
            if ($total2 > 0) {?>
                <select name="agentid" class="W150 size2 picmiddle">
                <option value="">不限门店</option>
				<?php
                for($j=0;$j<$total2;$j++) {
                    $rows2 = $db->fetch_array($rt2,'num');
                    if(!$rows2) break;
                    $clss=($agentid==$rows2[0])?' selected':'';?><option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[1],'out');?></option><?php
                }?>
                </select>　<?php
            }?>
          <!---->
        <input name="Skey" type="text" maxlength="25" class="input size2 W200" placeholder="按红娘ID号和姓名" value="<?php echo $Skey; ?>">　
        <?php }?>
        <input name="Skey2" type="text" maxlength="25" class="input size2 W200" placeholder="按客户UID、姓名和手机号码" value="<?php echo $Skey2; ?>">　
        <input placeholder="起始日期" name="sDATE1" id="sDATE1" type="text"  class="input size2 W100" value="<?php echo (empty($sDATE1))?'':$sDATE1; ?>" size="10" maxlength="10" autocomplete="off">
        <b>～</b> 
        <input placeholder="结束日期" name="sDATE2" id="sDATE2" type="text"  class="input size2 W100" value="<?php echo (empty($sDATE2))?'':$sDATE2; ?>" size="10" maxlength="10" autocomplete="off">　
        
       <!-- <script>nulltext = '通话状态';zeai_cn__CreateFormItem('select','qxflag','<?php echo $qxflag; ?>','class="size2 picmiddle"',[{i:"1",v:"空号错号"},{i:"2",v:"无人接听"},{i:"3",v:"客户拒接"},{i:"4",v:"成功接通"},{i:"5",v:"其它"}]);</script>-->
        <button type="submit" class="btn size2 QING"><i class="ico">&#xe6c4;</i> 筛选</button>
        <input type="hidden" name="p" value="<?php echo $p;?>" />
        <input type="hidden" name="k" value="<?php echo $k;?>" />
    </form>
    </div>
    <!---->
	<?php
    if ($total <= 0 ) { ?>
  <div class='nodataico'><i></i>暂无外呼记录        
        </div>
        <?php
    } else {
        $page_skin=2;$pagemode=3;$pagesize=$_ADM['admPageSize'];require_once ZEAI.'sub/page.php';?>
        <table class="tablelist">
        <form id="www_zeai_cn_FORM" method="get" action="<?php echo SELF; ?>">
        <tr>
        <th width="30"><input type="checkbox" id="checkboxall1" class="checkskin checkboxall"><label for="checkboxall1" class="checkskin-label"><i class="i1"></i></label></th>
        <th width="130">红娘</th>
        <th align="left"></th>
        <th align="left">总呼出数(次)</th>
        <th align="left">接通数(次)</th>
        <th align="left">接通率(%)</th>
        <th align="left">有效数(次)</th>
        <th align="left">有效接通率(%)</th>
        <th align="left">总呼总时长(秒)</th> 
        <th align="left">平均时长(秒)</th>
        <th align="left">未拔打剩余数量(人)</th>
        <th width="11" align="left">&nbsp;</th>
        </tr>
        <?php
        for($i=1;$i<=$pagesize;$i++) {
            $rows = $db->fetch_array($rt,'name');
            if(!$rows) break;
            $id = $rows['id'];
			$admid = $rows['admid'];
			$uid = $rows['uid'];
            $admname = dataIO($rows['admname'],'out');
            $znum     = intval($rows['znum']);
            $ztalktime = intval($rows['ztalktime']);			
			$yxznum    = $db->COUNT(__TBL_TALK__,$SQL." AND admid=".$admid." AND talktime>=30 AND flag=4");
			$jznum     = $db->COUNT(__TBL_TALK__,$SQL." AND admid=".$admid." AND flag=4");			
        ?>
        <tr id="tr<?php echo $id;?>">
            <td width="30" height="60"><input type="checkbox" name="list[]" value="<?php echo $id; ?>" uid="<?php echo $uid; ?>" id="id<?php echo $id; ?>" class="checkskin"><label for="id<?php echo $id; ?>" class="checkskin-label"><i class="i1"></i></label>
            </td>
          <td width="130"><?php echo $admname.' (id:'.$admid.')';?></td>
          <td align="left" class="lineH150"></td>
          <td height="60" align="left" class="lineH200"><?php echo $znum;?> </td>
          <td height="60" align="left" class="lineH200"><?php echo $jznum;?> </td>
          <td height="60" align="left" class="lineH200"><?php echo ($znum>0)?sprintf("%.1f",($jznum*100/$znum)):'0.0';?> %</td>
          <td height="60" align="left" class="lineH200"><?php echo $yxznum;?> </td>
          <td height="60" align="left" class="lineH200"><?php echo ($jznum>0)?sprintf("%.1f",($yxznum*100/$jznum)):'0.0';?> %</td>
          <td height="60" align="left" class="lineH200 "><?php echo show_his($ztalktime);?></td>
          <td height="60" align="left" class="lineH200 "><?php echo ($znum>0)?show_his(intval($ztalktime/$znum)):'0秒';?></td>
          <td height="60" align="left" class="lineH200 "><?php if(CRM_ADM){echo '<a class="aHUI" href="crm_talk_gh.php?Skey='.$admid.'">'.notel_num($admid).'</a>';}else{echo notel_num($admid);}?></td>         
          <td width="11" align="left" >&nbsp;</td>
          </tr>
        <?php } ?>
        </form>
    </table>
    <?php }?>
    <!---->
</div>


<?php if ($total > 0 ) {?>
<div class="listbottombox">
  <!--   <input type="checkbox" id="checkboxall2" class="checkskin checkboxall"><label for="checkboxall2" class="checkskin-label"><i class="i1"></i></label>　
   <button type="button" id="btndellist" class="btn size2 HEI2 disabled action">批量删除</button>　-->
    <input type="hidden" name="submitok" id="submitok" value="" />
    <?php echo '<div class="pagebox FR">'.$pagelist.'</div>'; ?>
</div>
<script>
var bg = '<?php echo $_Style['list_bg']; ?>',overbg = '<?php echo $_Style['list_overbg']; ?>',selectbg = '<?php echo $_Style['list_selectbg']; ?>';
</script>
<script src="js/zeai_tablelist.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<?php }?>
<script src="laydate/laydate.js"></script><script>lay('#version').html('-v'+ laydate.v);
laydate.render({elem: '#sDATE1'});
laydate.render({elem: '#sDATE2'});
</script>
<?php

function notel_num($admid) {
	global $db,$Skey,$agentid,$session_uid;
	//红娘适配
	$SQL2  = " (U.flag=1 OR U.flag=-2) AND U.kind<>4 AND U.admid>0 AND U.crm_ugrade=0 ";
	if (ifint($Skey)){
		$SQL2 .= " AND (U.admid=$Skey OR U.hnid=$Skey OR U.hnid2=$Skey) ";
	}elseif(!empty($Skey)){
		$SQL2 .= " AND (U.admname LIKE '%".$Skey."%' OR U.hnname LIKE '%".$Skey."%' OR U.hnname2 LIKE '%".$Skey."%' ) ";
	}
	//超管搜索按门店
	if (ifint($agentid) && CRM_ADM)$SQL2 .= " AND U.agentid=$agentid";
	
	if(ifint($admid) && !ifint($Skey)){
		$SQL2 .= " AND (U.admid=".$admid." OR U.hnid=".$admid." OR U.hnid2=".$admid.") ";
		//$SQL3 = " WHERE admid=".$admid;
	}else{
		if(!CRM_ADM)$SQL2 .= " AND (U.admid=".$session_uid." OR U.hnid=".$session_uid." OR U.hnid2=".$session_uid.") ";//我的
	}
	$rtt = $db->query("SELECT COUNT(*) FROM ".__TBL_USER__." U WHERE ".$SQL2." AND U.id NOT IN (SELECT uid FROM ".__TBL_TALK__.$SQL3.")    ");
	$roww = $db->fetch_array($rtt);
	$notel_num = $roww[0];	
	return $notel_num;
}
require_once 'bottomadm.php';?>